if (SERVER) then

	AddCSLuaFile( "shared.lua" )
	SWEP.Weight				= 5
	SWEP.AutoSwitchTo		= false
	SWEP.AutoSwitchFrom		= false
	SWEP.HoldType			= "slam"

end

if ( CLIENT ) then

	SWEP.DrawAmmo			= false
	SWEP.DrawCrosshair		= false
	SWEP.ViewModelFOV		= 75
	SWEP.ViewModelFlip		= true
	SWEP.CSMuzzleFlashes	= false
	
	SWEP.IconFont = "CSSelectIcons"
	
	SWEP.PrintName = "Construction Kit"
	SWEP.Slot = 1
	SWEP.Slotpos = 1
	SWEP.IconLetter = "p"

end

SWEP.Base = "tp_base"

SWEP.Spawnable			= false
SWEP.AdminSpawnable		= true

SWEP.Primary.ClipSize		= -1
SWEP.Primary.DefaultClip	= -1
SWEP.Primary.Automatic		= false
SWEP.Primary.Ammo			= "none"

SWEP.Secondary.ClipSize		= -1
SWEP.Secondary.DefaultClip	= -1
SWEP.Secondary.Automatic	= false
SWEP.Secondary.Ammo			= "none"

SWEP.ViewModel = "models/weapons/v_eq_smokegrenade.mdl"
SWEP.WorldModel = "models/weapons/w_eq_smokegrenade.mdl"

SWEP.Click = Sound("buttons/lightswitch2.wav")
SWEP.Deny = Sound("HL1/fvox/buzz.wav")
SWEP.Hammer = GameSounds.WoodHammer
SWEP.Drill = GameSounds.Drill

SWEP.Anim = ACT_VM_PULLBACK_LOW

SWEP.BModel = "models/props_debris/wood_chunk03b.mdl"
SWEP.BModels = {"models/props_debris/wood_chunk03a.mdl","models/props_debris/wood_chunk03b.mdl"}

SWEP.Offset = math.random(-3,3)
SWEP.Position = 37 //distance

SWEP.MNum = 2

function SWEP:Initialize()
    if (SERVER) then
		self:SetWeaponHoldType(self.HoldType)
	end
	util.PrecacheModel(self.BModel)
end 

function SWEP:GetUsedAmmoType()
	return "none"
end

function SWEP:Deploy()
	self.Weapon:SendWeaponAnim(ACT_VM_DRAW)
	self.MNum = 2
	self.Position = 37
	self.BModel = "models/props_debris/wood_chunk03b.mdl"
	if CLIENT then
		self:ReleaseGhost()
	end
	return true
end  

function SWEP:Holster()
	self:ReleaseGhost()
	return true
end

function SWEP:ReleaseGhost()
	if ( self.GhostEntity ) then
		if (!self.GhostEntity:IsValid()) then self.GhostEntity = nil return end
		self.GhostEntity:Remove()
		self.GhostEntity = nil
	end
	if ( self.GhostOffset ) then
		for k,v in pairs( self.GhostOffset ) do
			self.GhostOffset[k] = nil
		end
	end
end

function SWEP:MakeGhost( model, pos, angle )

	self.GhostEntity = ents.Create( "prop_physics" )
	
	if (!self.GhostEntity:IsValid()) then
		self.GhostEntity = nil
		return
	end
	
	self.GhostEntity:SetModel( model )
	self.GhostEntity:SetPos( pos )
	self.GhostEntity:SetAngles( angle )
	self.GhostEntity:Spawn()
	
	self.GhostEntity:SetSolid( SOLID_VPHYSICS );
	self.GhostEntity:SetMoveType( MOVETYPE_NONE )
	self.GhostEntity:SetNotSolid( true );
	self.GhostEntity:SetRenderMode( RENDERMODE_TRANSALPHA )
	self.GhostEntity:SetColor( 255, 255, 255, 200 )
	
end

function SWEP:UpdateGhost()

	if (self.GhostEntity == nil) then return end
	if (!self.GhostEntity:IsValid()) then self.GhostEntity = nil return end
	
	local tr = utilx.GetPlayerTrace( self:GetOwner(), self:GetOwner():GetCursorAimVector() )
	local trace = util.TraceLine( tr )
	if (!trace.Hit) then return end
	
	local ang = (trace.HitNormal * -1):Angle() + Angle(0,0,90 + self.Offset)
	local pos = trace.HitPos + trace.HitNormal
	
	local trlength = self.Weapon:GetOwner():GetPos() - trace.HitPos
	trlength = trlength:Length() 
	
	if trlength < 110 then
		self.GhostEntity:SetColor( 50, 255, 50, 200 )
	else
		self.GhostEntity:SetColor( 255, 50, 50, 200 )
	end
	
	if not trace.HitWorld and not trace.Entity:GetClass() == "prop_door_rotating" then
		self.GhostEntity:SetColor( 255, 50, 50, 200 )
		self.GhostEntity:SetModel(self.BModel)
		self.GhostEntity:SetPos( pos + (self.GhostEntity:GetUp() * self.Position) )
		self.GhostEntity:SetAngles( ang )
	elseif string.find(trace.Entity:GetClass(),"prop_door") then
		self.GhostEntity:SetModel("models/props_c17/tools_wrench01a.mdl")
		self.GhostEntity:SetPos(trace.HitPos + trace.HitNormal * 5)
		self.GhostEntity:SetAngles((trace.HitNormal * -1):Angle() + Angle(0,90,90))
	else
		self.GhostEntity:SetModel(self.BModel)
		self.GhostEntity:SetPos( pos + (self.GhostEntity:GetUp() * self.Position) )
		self.GhostEntity:SetAngles( ang )
	end
	
end

function SWEP:SetPlacePosition(ent)

	local tr = utilx.GetPlayerTrace( self:GetOwner(), self:GetOwner():GetCursorAimVector() )
	local trace = util.TraceLine( tr )
	if (!trace.Hit) then return end
	
	local ang = (trace.HitNormal * -1):Angle() + Angle(0,0,90 + self.Offset)
	ent:SetAngles( ang )
	
	local pos = trace.HitPos + trace.HitNormal
	ent:SetPos( pos + (ent:GetUp() * self.Position) )
	//ent:SetPos(self.Owner:GetPos())
	
	local phys = ent:GetPhysicsObject()
	if phys:IsValid() then
		phys:EnableMotion( false )
	end

end

function SWEP:DrawHUD()
	
	if not self.GhostEntity then
		self:MakeGhost(self.BModel,self.Owner:GetPos() + Vector(0,0,100), Angle(0,0,0))
	else
		self:UpdateGhost()
	end

	local w,h = ScrW(),ScrH()
	
	surface.SetDrawColor(220, 180, 0, 220)
	local wh, lh, sh = w*.5, h*.5, 10
	surface.DrawLine(wh - sh, lh - sh, wh + sh, lh - sh) //top line
	surface.DrawLine(wh - sh, lh + sh, wh + sh, lh + sh) //bottom line
	surface.DrawLine(wh - sh, lh - sh, wh - sh, lh + sh) //left line
	surface.DrawLine(wh + sh, lh - sh, wh + sh, lh + sh) //right line
end

function SWEP:Think()
	if not self.BuildTime then return end
	if self.BuildTime < CurTime() then
		self:BarricadeTrace()
		self.BuildTime = nil
	end
end

function SWEP:CanPrimaryAttack()
	return true
end

function SWEP:GetWood()
	local inv = self.Owner:GetItemInventory()
	local valids = {}
	for k,v in pairs(inv) do
		local tbl = Inventory.FindTable(v)
		if tbl.Name == "Wood Chunk" then
			table.insert(valids,tbl.Model)
		end
	end
	return valids
end

function SWEP:BarricadeTrace()

	if CLIENT then return end

	local trace = util.GetPlayerTrace(self.Owner)
	local tr = util.TraceLine(trace)
	local trlength = self.Weapon:GetOwner():GetPos() - tr.HitPos
	trlength = trlength:Length() 
	   
	if trlength < 110 and tr.HitWorld then
	
		local dt = {}
		dt.start = tr.HitPos + tr.HitNormal * 8
		dt.endpos = dt.start + Vector(0,0,-1000)
		dt.filter = self.Owner
		local dr = util.TraceLine(dt)
		if tr.HitPos:Distance(dr.HitPos) > 150 then
			Inventory.Notice(self.Owner, "Go fuck yourself", Inventory.Red)
			self.Owner:DoBleed()
			self.Weapon:SendWeaponAnim(ACT_VM_DRAW)
			return
		end
		
		local valids = self:GetWood()
		if table.Count(valids) >= 2 then
					
			self.Owner:TakeStamina(30)
			self.Owner:AddAwardPoints(AWARD_GOOD_ENGINEER,1)
			//self.Owner:AddPoints(2)
			self.Owner:EmitSound(ChooseRandom(self.Drill),100,math.random(90,110))
			self.Owner:EmitSound(ChooseRandom(self.Hammer),100,math.random(90,110))
			Inventory.Notice(self.Owner, "Created a barricade", Inventory.Green)
			Inventory.Notice(self.Owner, "Toolkit used up 2 items", Inventory.Red)
					
			self.Owner:RemoveFromInventory(valids[1])
			self.Owner:RemoveFromInventory(valids[2])
			
			//spawn the board
			
			local item = ents.Create("prop_physics")
			item:SetModel(self.BModel)
			item:Spawn()
			item:SetHealth(350)
			item.Builder = self.Owner
			self:SetPlacePosition(item)
			
			if not self.Owner.DoorHint then
				self.Owner.DoorHint = true
				Inventory.Notice(self.Owner, "You can repair doors with your kit", Inventory.White)
			end
					
		else
			Inventory.Notice(self.Owner, "You don't have enough items", Inventory.Red)
			self.Owner:EmitSound(self.Deny)
			if not self.Owner.PropsHint then
				self.Owner.PropsHint = true
				Inventory.Notice(self.Owner, "You need to find 2 wood chunks", Inventory.White)
			end
		end
		
	elseif string.find(tr.Entity:GetClass(),"prop_door") then
	
		local valids = self:GetWood()
		if table.Count(valids) >= 1 then
			if not tr.Entity.HitHealth then
				self.Owner:TakeStamina(20)
				self.Owner:AddAwardPoints(AWARD_GOOD_ENGINEER,1)
				self.Owner:EmitSound(ChooseRandom(self.Drill),100,math.random(90,110))
				self.Owner:EmitSound(ChooseRandom(self.Hammer),100,math.random(90,110))
				Inventory.Notice(self.Owner, "Reinforced this door", Inventory.Green)
				Inventory.Notice(self.Owner, "Toolkit used up 1 item", Inventory.Red)
				self.Owner:RemoveFromInventory(valids[1])
				tr.Entity.HitHealth = 80
			elseif tr.Entity.HitHealth <= 20 then
				self.Owner:TakeStamina(20)
				self.Owner:AddAwardPoints(AWARD_GOOD_ENGINEER,1)
				self.Owner:EmitSound(ChooseRandom(self.Drill),100,math.random(90,110))
				self.Owner:EmitSound(ChooseRandom(self.Hammer),100,math.random(90,110))
				Inventory.Notice(self.Owner, "Repaired this door", Inventory.Green)
				Inventory.Notice(self.Owner, "Toolkit used up 1 item", Inventory.Red)
				self.Owner:RemoveFromInventory(valids[1])
				tr.Entity.HitHealth = 60
			else
				Inventory.Notice(self.Owner, "That door isn't damaged", Inventory.Red)
				self.Owner:EmitSound(self.Deny)
			end
		else
			Inventory.Notice(self.Owner, "You don't have enough items", Inventory.Red)
			self.Owner:EmitSound(self.Deny)
		end
		
	else
		Inventory.Notice(self.Owner, "You can't place a barricade here", Inventory.Red)
		self.Owner:EmitSound(self.Deny)
	end
	self.Weapon:SendWeaponAnim(ACT_VM_DRAW)
end

SWEP.PrimaryDelay = 1
function SWEP:CanPrimaryAttack()
	return CurTime() > self.PrimaryDelay
end

function SWEP:PrimaryAttack()
	if not self:CanPrimaryAttack() then return end
	self.PrimaryDelay = CurTime() + 1.5
	self.Weapon:SendWeaponAnim(self.Anim)
	self.BuildTime = CurTime() + .9
 end
 
SWEP.SecondDelay = 1
function SWEP:CanSecondaryAttack()
	return CurTime() > self.SecondDelay
end
 
function SWEP:SecondaryAttack()
	if not self:CanSecondaryAttack() then return end
	self.SecondDelay = CurTime() + 1 
	self.Weapon:EmitSound(self.Click)
	self.Offset = math.Rand(-3,3)
	self.Position = self.Position * -1
	self.MNum = self.MNum + 1
	if self.MNum > 2 then
		self.MNum = 1
	end
	self.BModel = self.BModels[self.MNum]
end

